/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Presentacion;

import Mail.Archivo;
import Mail.ListaMensajes;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import Mail.ListaServidores;
import Mail.ListaUsuarios;
import Mail.MailServer;
import javax.swing.DefaultListModel;
import javax.swing.JList;

/**
 *
 * @author Bery
 */
public class FormServidor extends javax.swing.JFrame {

    public ListaServidores listaServer = new ListaServidores();
    public Archivo archivo = new Archivo();

    /**
     * Creates new form Servidor
     */
    public FormServidor() {

        initComponents();
        this.CargarListaServidores();
    }

    public FormServidor(Boolean bFalse) {
        initComponents();
        this.CargarListaServidores();
    }

    private void CargarListaServidores() {
        Object objectos = (archivo.leer("servidores"));
        if (objectos != null) {
            listaServer = (ListaServidores) (objectos);

            DefaultListModel modelo = new DefaultListModel();
            String[] servidores = listaServer.obtenerServidoress();
            for (int i = 0; i < servidores.length; i++) {
                modelo.addElement(servidores[i]);
            }
            this.ListaUser.setModel(modelo);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        TextNam = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        ListaUser = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        BtnGuardar = new javax.swing.JButton();
        VerServidor = new javax.swing.JButton();
        BtnNuevo = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("CONFIGURAR SERVIDORES - SES-GPS");
        setResizable(false);

        jLabel3.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel3.setText("Nombre del Servidor:");

        jScrollPane1.setViewportView(ListaUser);

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
        jLabel1.setText("Configurar Servidores");

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        BtnGuardar.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        BtnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/guardar.png"))); // NOI18N
        BtnGuardar.setText("Guardar Servidor");
        BtnGuardar.setName("Eliminar"); // NOI18N
        BtnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnGuardarActionPerformed(evt);
            }
        });

        VerServidor.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        VerServidor.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/planilla2.jpg"))); // NOI18N
        VerServidor.setText("Ver Usuarios");
        VerServidor.setName("Eliminar"); // NOI18N
        VerServidor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                VerServidorActionPerformed(evt);
            }
        });

        BtnNuevo.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        BtnNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/on.png"))); // NOI18N
        BtnNuevo.setText("Iniciar Sesion");
        BtnNuevo.setName("NuevoUser"); // NOI18N
        BtnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnNuevoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(BtnGuardar, javax.swing.GroupLayout.DEFAULT_SIZE, 209, Short.MAX_VALUE)
                    .addComponent(VerServidor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(BtnNuevo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(BtnGuardar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(VerServidor)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(BtnNuevo)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel4.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/planilla1.jpg"))); // NOI18N
        jLabel4.setText("Lista de Servidores");

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/1.jpg"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(64, 64, 64))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(18, 18, 18)
                                .addComponent(TextNam, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap(25, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel2))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(39, 39, 39)
                        .addComponent(jLabel1)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(TextNam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BtnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnGuardarActionPerformed
        // TODO add your handling code here:
        if (this.TextNam.getText().isEmpty()) {
            JOptionPane.showMessageDialog(this, "Debe introducir un nombre de servidor", "ERROR", 0);
            return;
        }
        //cargamos a la lista de servidores;crear un archivo con ese nombre del servidor
        this.listaServer.add(this.TextNam.getText());
        archivo.guardar(listaServer, "servidores");

        archivo.guardar(null, this.TextNam.getText());

        JOptionPane.showMessageDialog(this, "Datos introducidos correctamente", "SES", 1);
        this.CargarListaServidores();
        this.Limpiar();

    }//GEN-LAST:event_BtnGuardarActionPerformed

    private void BtnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnNuevoActionPerformed
        // TODO add your handling code here:
        FormAutenticacion inicioSesion = new FormAutenticacion();
        Dimension dlgSize = inicioSesion.getPreferredSize();
        Dimension pantalla = getSize();
        Dimension ventana = inicioSesion.getSize();
        inicioSesion.setLocation((pantalla.width - inicioSesion.WIDTH) / 2, (pantalla.height - inicioSesion.HEIGHT) / 2);
        inicioSesion.setLocationRelativeTo(null); // CENTRA EL SEGUNDO FRAME EN LA PANTA
        inicioSesion.pack();
        inicioSesion.setResizable(true); // PERMITE REDIMENSIONARLO O AGRANDARL
        inicioSesion.setVisible(true);
        this.setVisible(false);

    }//GEN-LAST:event_BtnNuevoActionPerformed

    private void VerServidorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_VerServidorActionPerformed
        // TODO add your handling code here:
        if (this.ListaUser.isSelectionEmpty()) {
            JOptionPane.showMessageDialog(this, "Debe seleccionar un registro de servidor", "ERROR", 0);
            return;
        }
        FormAdministracionServidor detalleForm = new FormAdministracionServidor();
        Dimension dlgSize = detalleForm.getPreferredSize();
        Dimension pantalla = getSize();
        Dimension ventana = detalleForm.getSize();
        detalleForm.setLocation((pantalla.width - detalleForm.WIDTH) / 2, (pantalla.height - detalleForm.HEIGHT) / 2);
        detalleForm.setLocationRelativeTo(null); // CENTRA EL SEGUNDO FRAME EN LA PANTA
        detalleForm.pack();
        // detalleForm.setResizable(true); // PERMITE REDIMENSIONARLO O AGRANDARL
        detalleForm.setVisible(true); // ESTO HACE QUE SE VEA EL SEGUNDO JFRAME.. 
        //paso valores
        detalleForm.nomServer.setText(this.ListaUser.getSelectedValue().toString());
        Object objectos = (archivo.leer(this.ListaUser.getSelectedValue().toString()));
        if (objectos != null) {
            detalleForm.listaUsuarios = (ListaUsuarios) (objectos);
            DefaultListModel modelo = new DefaultListModel();
            String[] usuarios = detalleForm.listaUsuarios.obtenerUsuarios();
            for (int i = 0; i < usuarios.length; i++) {
                modelo.addElement(usuarios[i]);
            }
            detalleForm.ListaUser.setModel(modelo);
        }

        this.setVisible(false);

    }//GEN-LAST:event_VerServidorActionPerformed

    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        /* Set the Nimbus look and feel */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(FormServidor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(FormServidor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(FormServidor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(FormServidor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        /* Create and display the form */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new FormServidor().setVisible(true);
//            }
//        });
//    }
    private void Limpiar() {
        this.TextNam.setText(null);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtnGuardar;
    private javax.swing.JButton BtnNuevo;
    private javax.swing.JList ListaUser;
    private javax.swing.JTextField TextNam;
    private javax.swing.JButton VerServidor;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
